<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\UserIncome;
use app\common\model\UserIncomeLog;
use EasyWeChat\Factory;


class Share extends Api
{
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';

    //
    public function index()
    {
        //推广人数
        //收益
        //等级
        $income =UserIncome::get(['user_id' => $this->auth->id]);
        $data = [
            'share_num' => $income ? $income->share_num : 0,
            'share_money' => $income ? $income->share_money : 0,
            'level_text' => '普通',
            'check_state' => $income ? $income->check_state : -1,
        ];
//        $income->level_text = '普通';
        $this->success(__('成功'),$data);
    }

    //收益明细
    public function moneylist()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $model = new UserIncomeLog();
        $list =  $model
            ->with(['user'])->where(['user_id' => $this->auth->id])->order('id desc')->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    //申请代理
    public function apply()
    {
        $income = UserIncome::get(['user_id' => $this->auth->id]);
        $ret = true;
        if(!empty($income)) {
            if($income->check_state == 0) {
                $this->error('申请中');
            }elseif ($income->check_state == 2) {
                $ret =  $income->save([
                    'check_state' => 0
                ]);
            }else {
                $this->error('已认证');
            }
        }else {
            $ret = UserIncome::create([
                'check_state' => 0,
                'user_id' => $this->auth->id
            ]);
        }
        if($ret) {
            $this->success('成功');
        }else {
            $this->error('失败');
        }
    }

    //推广记录
    public function list()
    {
        $page = $this->request->get('page');
        $limit = $this->request->get('limit');
        $model = new \app\common\model\User();
        $list =  $model->where(['p_id' => $this->auth->id])->order('id desc')->paginate($limit,false,['page' => $page]);

        $result = array("total" => $list->total(), "rows" => $list->items());
        $this->success('返回成功',$result);
    }

    //生成二维码
    public function createposter()
    {
        $this->success();
    }

    public function qrcode()
    {
        $income = UserIncome::get(['user_id' => $this->auth->id, 'check_state' => 1]);
        if(empty($income)) $this->error('请申请推广');
        if(!empty($income['codeimage'])){
            $config = get_addon_config('third');

            $config = [
                'app_id' =>  $config['wechatweb']['app_id'], // 小程序ID
                'secret' => $config['wechatweb']['app_secret'], // 密码
                // 下面为可选项
                // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
                'response_type' => 'array',
                'log' => [
                    'level' => 'debug',
                    'file' => __DIR__.'/wechat.log',
                ],
            ];

            $app = Factory::miniProgram($config);

            $response  = $app->app_code->getUnlimit($this->auth->id, [
                'page'  => 'pages/home/home',
                'width' => 600,
            ]);
            $share_img = "";
            // 司机码保存到本地
            if ($response instanceof \EasyWeChat\Kernel\Http\StreamResponse) {
                $tmp_bg_image = 'assets/web/home/poster.jpg';//背景图路径

                $filename = $response->save(ROOT_PATH.'public/uploads/drivingcode');
                $qrcode_img = 'uploads/drivingcode/'.$filename;
                $share_path = 'uploads/share/';
                is_dir($share_path) OR mkdir($share_path, 0777, true);
                $share_img = $share_path.'_'.$income->id.'.png';
                composite_picture($tmp_bg_image, $qrcode_img, $share_img, true, '50', '50', true, '', 300, 580);
                //模板背景, 二维码, 海报, 二维码是否缩小, 二维码缩小的宽度，二维码缩小的高度，是否等比例缩放, 文字, 二维码在x轴的位置, 二维码在y轴的位置

                \app\common\model\UserIncome::where('user_id',$this->auth->id)->update(['codeimage'=>'/'.$share_img]);
            }
            $this->success('',cdnurl('/'.$share_img));
        } else {
            $this->success('',cdnurl($income['codeimage']));
        }

    }

}